package demo1;

import demo1.ILinkList;

public class MyLinkedList implements ILinkList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public  ListNode head;
    public  ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode list=new ListNode(data);
        if(head==null){
            head=last=list;
            return;
        }else{
            list.next=head;
            head.prev=list;
            head=list;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode list=new ListNode(data);
        if(head==null){
            head=last=list;
            return;
        }else{
            last.next=list;
            list.prev=last;
            last=last.next;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode list=new ListNode(data);
        ListNode cur=head;
        if(index<0||index>size()){
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        } else if (index==size()) {
            addLast(data);
            return;
        }else {
            while((index-1)!=0){
                cur=cur.next;
                index--;
            }
                cur.next.prev=list;
                list.prev=cur;
                list.next=cur.next;
                cur.next=list;
            }
        }


    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    //只有一个节点的时候
                   /* if(head!=null){
                        head.prev=null;
                    }*/
                } else if (cur==last) {
                    last=last.prev;
                    last.next=null;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
                return;
            }else{
                cur=cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    //只有一个节点的时候
                    if(head!=null){
                        head.prev=null;
                    }
                } else if (cur==last) {
                    last=last.prev;
                    last.next=null;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
                cur=cur.next;
            }else{
                cur=cur.next;
            }
        }
    }

    @Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.prev=null;
            cur.next=null;
            cur=curN;
        }
        head=last=null;
    }
}
